package com.mhkj.common.timer.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import com.mhkj.common.enums.PlanSate;
import com.mhkj.revise.plan.entity.PlanNode;
import com.mhkj.revise.professional.service.ProfessionalPlanNodeService;
import com.mics.core.common.util.DateUtils;

/**
 * 自动延期
 * @author mics
 * @date 2018年8月23日
 * @version 1.0
 */
@Service
public class AutoDelayService {
	@Resource
	private ProfessionalPlanNodeService professionalPlanNodeService;
	
	/**
	 * 自动延期
	 * @author mics
	 * @date 2018年8月23日
	 * @version 1.0
	 */
	@Transactional
	public void delay(){
		List<PlanNode> planNodes = professionalPlanNodeService.listAllChecked();
		Date now = DateUtils.getDateYMD(new Date(), "yyyy-MM-dd");
		for(PlanNode planNode:planNodes){ //如果当前逾期，并且没有延期过，进行两天的自动延期
			if(planNode.getState() != PlanSate.compline.getState() && planNode.getState() != PlanSate.OverCompline.getState() && now.equals(planNode.getEndDate()) && planNode.getDelayDay() == 0){ 
				List<PlanNode> planNodeTemps = professionalPlanNodeService.getSameNameNode(planNode.getId());
				for(PlanNode planNodeTmep:planNodeTemps){
					planNodeTmep.setDelayDate(DateUtils.addDay(planNodeTmep.getEndDate(), 2));
					planNodeTmep.setDelayNum(planNodeTmep.getDelayNum()+1);
					planNodeTmep.setDelayDay(2);
					professionalPlanNodeService.edit(planNodeTmep);
				}
				
			}
		}

	}
}
